package com.zy.storage

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import com.zy.storage.doc.DocInfo
import com.zy.storage.doc.DocInfoDao

//警告: Schema export directory is not provided to the annotation processor so we cannot export the schema. You can either provide `room.schemaLocation` annotation processor argument OR set exportSchema to false.
//@Database(entities = [DocInfo::class], version = 1, exportSchema = false)
@Database(entities = [DocInfo::class], version = 1)
abstract class ZYDatabase : RoomDatabase() {

    abstract fun docDao(): DocInfoDao

    companion object {
        // 防止同一时间创建多个实例
        @Volatile
        private var INSTANCE: ZYDatabase? = null

        fun getDatabase(context: Context): ZYDatabase {
            // 单例
            return INSTANCE ?: synchronized(this) {
                val instance = Room.databaseBuilder(
                    // 可以使用单例Application来代替此参数传递
                    context.applicationContext,
                    ZYDatabase::class.java,
                    "zy_diy_db" //数据库名称
                ).build()
                INSTANCE = instance

                instance
            }
        }
    }
}